//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ysy.tz.core.print;

import com.ysy.tz.core.engine.CustomerFXMLLoader;
import com.ysy.tz.core.utils.FileResourceLoader;
import com.ysy.tz.core.utils.PrintUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import org.apache.commons.collections4.CollectionUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultProcessingInstruction;

public abstract class PrintWorker {
    int printStopTime;
    int printDpi;
    int paperWidthMM;
    int paperHeightMM;
    private LinkedBlockingQueue<HashMap<String, String>> printQueue = new LinkedBlockingQueue();
    private Pane printRegion;
    PrintWorkThread printWorkThread;

    public PrintWorker() {
    }

    public void startPrint() {
        this.printWorkThread = new PrintWorkThread();
        this.printWorkThread.start();
    }

    public void stopPrint() {
        this.printWorkThread.interrupt();
    }

    public void loadTemplate(String templatePath, String... packages) throws Exception {
        InputStream inputStream = FileResourceLoader.loadInputStream(templatePath);
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputStream);
        String[] var6 = packages;
        int var7 = packages.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            String aPackage = var6[var8];
            document.add(new DefaultProcessingInstruction("import", aPackage));
        }

        Element rootElement = document.getRootElement();
        rootElement.setName("AnchorPane");
        Element printSettingNode = rootElement.element("PrintSetting");
        this.printStopTime = Integer.parseInt(printSettingNode.attribute("stopTime").getValue());
        this.printDpi = Integer.parseInt(printSettingNode.attribute("dpi").getValue());
        this.paperWidthMM = Integer.parseInt(printSettingNode.attribute("paperWidthMM").getValue());
        this.paperHeightMM = Integer.parseInt(printSettingNode.attribute("paperHeightMM").getValue());
        rootElement.remove(printSettingNode);
        rootElement.elements().forEach((element) -> {
            Attribute attribute = element.attribute("left");
            if (attribute != null) {
                element.remove(attribute);
                element.addAttribute("AnchorPane.leftAnchor", attribute.getValue());
            }

            attribute = element.attribute("top");
            if (attribute != null) {
                element.remove(attribute);
                element.addAttribute("AnchorPane.topAnchor", attribute.getValue());
            }

            attribute = element.attribute("right");
            if (attribute != null) {
                element.remove(attribute);
                element.addAttribute("AnchorPane.rightAnchor", attribute.getValue());
            }

            attribute = element.attribute("bottom");
            if (attribute != null) {
                element.remove(attribute);
                element.addAttribute("AnchorPane.bottomAnchor", attribute.getValue());
            }

        });
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        XMLWriter xmlWriter = new XMLWriter(stream) {
            public void write(Document doc) throws IOException {
                this.writeDeclaration();
                if (doc.getDocType() != null) {
                    this.indent();
                    this.writeDocType(doc.getDocType());
                }

                int i = 0;
                ArrayList<Node> nodeList = new ArrayList();

                Node node;
                for(int size = doc.nodeCount(); i < size; ++i) {
                    node = doc.node(i);
                    if (node instanceof ProcessingInstruction) {
                        this.writeNode(node);
                        this.writePrintln();
                    } else {
                        nodeList.add(node);
                    }
                }

                Iterator var6 = nodeList.iterator();

                while(var6.hasNext()) {
                    node = (Node)var6.next();
                    this.writeNode(node);
                }

                this.writePrintln();
                this.flush();
            }
        };
        xmlWriter.write(document);
        byte[] strBytes = stream.toByteArray();
        InputStream inputStream2 = new ByteArrayInputStream(strBytes);
        this.printRegion = (Pane)(new CustomerFXMLLoader()).load(inputStream2);
    }

    public void addPrintObject(HashMap<String, String> valueMap) {
        try {
            this.printQueue.put(valueMap);
        } catch (InterruptedException var3) {
            var3.printStackTrace();
        }

    }

    public void loopRegion(Pane pane, HashMap<String, String> valueMap) {
        ObservableList<javafx.scene.Node> nodes = pane.getChildren();
        if (CollectionUtils.isNotEmpty(nodes)) {
            Iterator var4 = nodes.iterator();

            while(var4.hasNext()) {
                javafx.scene.Node node = (javafx.scene.Node)var4.next();
                if (node.getId() != null) {
                    this.setNodeValue(node, valueMap);
                }

                if (node instanceof Pane) {
                    this.loopRegion((Pane)node, valueMap);
                }
            }
        }

    }

    public abstract void setNodeValue(javafx.scene.Node var1, HashMap<String, String> var2);

    class PrintWorkThread extends Thread {
        PrintWorkThread() {
        }

        public void run() {
            while(!Thread.currentThread().isInterrupted()) {
                try {
                    HashMap<String, String> values = (HashMap)PrintWorker.this.printQueue.take();
                    Platform.runLater(() -> {
                        try {
                            PrintWorker.this.loopRegion(PrintWorker.this.printRegion, values);
                            if (PrintWorker.this.printRegion.getScene() == null) {
                                new Scene(PrintWorker.this.printRegion, PrintWorker.this.printRegion.getPrefWidth(), PrintWorker.this.printRegion.getPrefHeight());
                            } else {
                                AnchorPane anchorPane = new AnchorPane();
                                anchorPane.getChildren().addAll(PrintWorker.this.printRegion.getChildren());
                                anchorPane.setPrefWidth(PrintWorker.this.printRegion.getPrefWidth());
                                anchorPane.setPrefHeight(PrintWorker.this.printRegion.getPrefHeight());
                                new Scene(anchorPane, PrintWorker.this.printRegion.getPrefWidth(), PrintWorker.this.printRegion.getPrefHeight());
                                PrintWorker.this.printRegion = anchorPane;
                            }

                            System.out.println("区域大小 停 --->>> " + PrintWorker.this.printStopTime + "," + PrintWorker.this.printRegion.getChildren().size() + "," + PrintWorker.this.printRegion.getScene().getWidth() + "," + PrintWorker.this.printRegion.getScene().getHeight());
                            PrintUtils.printNode(PrintWorker.this.printRegion, PrintWorker.this.paperWidthMM, PrintWorker.this.paperHeightMM, PrintWorker.this.printDpi, 0);
                        } catch (Exception var3) {
                            var3.printStackTrace();
                        }

                    });
                    Thread.sleep((long)PrintWorker.this.printStopTime);
                } catch (InterruptedException var2) {
                    var2.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }

        }
    }
}
